﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Clarity.Phone.Controls;
using Clarity.Phone.Extensions;
using TradeMeSDK;

namespace TradeMo
{
    public class SelectorItem<T>
    {
        public string Title { get; set; }
        public string Picture { get; set; }
        public T Item { get; set; }
    }

    public class SelectorHelper<T> 
    {
        private readonly Panel _overlayPanel;
        private Action _selectedAction;

        private readonly QuickJumpGridOverlay _overlay;
        private readonly Popup _popup;

        public bool IsOverlayOpen { get; set; }
        public SelectorItem<T> Selected { get; private set; }

        public SelectorHelper()
        {
            _overlayPanel = new Grid();
            _overlay = new QuickJumpGridOverlay(null);
            _overlay.TileSelected += OnOverlayTileSelected;

            _overlayPanel.Children.Add(_overlay);

            _popup = new Popup { Child = _overlayPanel };
        }

        private List<SelectorItem<T>> _items;
        public List<SelectorItem<T>> Items
        {
            get { return _items; }
            set 
            { 
                _items = value;
                _overlay.ItemsSource = _items; 
            }
        }

        public void OpenPopup(Panel panel, Action action)
        {
            _selectedAction = action;

            if (IsOverlayOpen)
                return;
            IsOverlayOpen = true;
            UpdateOverlayPlacement();

            App.RootFrameworkElement.SizeChanged += HandleRootVisualSizeChanged;
            App.AppPage.BackKeyPress += OnBackKeyPress;

            _popup.InvokeOnLayoutUpdated(() => _overlay.Show());
            _popup.IsOpen = true;
            if (!panel.Children.Contains(_popup))
                panel.Children.Add(_popup);
        }

		public void ClosePopup()
		{
			if (IsOverlayOpen)
				_overlay.Close();
			else
				ClosePopup(string.Empty);
		}

        private void OnBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_popup.IsOpen)
            {
                e.Cancel = true;
                ClosePopup(string.Empty);
            }
        }

        void OnOverlayTileSelected(object sender, object item)
        {
            Selected = item as SelectorItem<T>;
            ClosePopup(item);
        }

        /// <summary>
        /// Closes the Popup.
        /// </summary>
        private void ClosePopup(object item)
        {
            App.AppPage.BackKeyPress -= OnBackKeyPress;
            App.RootFrameworkElement.SizeChanged -= HandleRootVisualSizeChanged;

            _popup.IsOpen = false;
            _selectedAction.OnUIThread();
            IsOverlayOpen = false;
        }

        private void HandleRootVisualSizeChanged(object sender, SizeChangedEventArgs e)
        {
            UpdateOverlayPlacement();
        }

        private void UpdateOverlayPlacement()
        {
            if ((App.RootFrameworkElement != null) && (_overlayPanel != null))
            {
                // Size the overlay to match the new container
                _overlayPanel.Width = App.RootFrameworkElement.ActualWidth;
                _overlayPanel.Height = App.RootFrameworkElement.ActualHeight;
            }
        }
    }
}
